<?php

namespace App\Models\V1;

use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;

// 模型编号：01

/**
 * @method static where(string $string, string $string1, int $ENABLE_TRUE)
 */
class Admin extends Authenticatable implements JWTSubject
{
    use HasRoles;

    /**
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * @var string
     */
    protected $table = 'admin_users';

    /**
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * @var string[]
     */
    protected $dates = [
        'current_login_time',
        'last_login_time',
        'expired_time',
    ];

    /**
     * @var string[]
     */
    protected $hidden = ['password'];

    const CREATED_AT = 'created_time';
    const UPDATED_AT = 'updated_time';

    // 是否为有效用户；0：否|1：是
    const ENABLE_TRUE  = 1;
    const ENABLE_FALSE = 0;

    // 是否为超级管理员；0：否|1：是
    const SUPER_TRUE  = 1;
    const SUPER_FALSE = 1;

    /**
     * @expalin 自动排序
     * @param Builder $query
     * @return Builder
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/29/15:53
     */
    public function scopeRecent(Builder $query): Builder
    {
        return $query->orderByDesc('id');
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * @expalin 判断是否为超级管理员
     * @return bool
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/30/11:08
     */
    public function isSuper()
    {
        return (bool) $this->is_super;
    }
}
